package com.abe.libquick.utils;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.util.ArrayList;
import java.util.List;

public class GetActivityView {

    private Activity context;

    public GetActivityView(Activity context) {
        this.context = context;
    }

    public List<View> getAllChildViews() {
        View view = context.getWindow().getDecorView();
        return getAllChildViews(view);
    }

    public List<View> getAllChildViews(View view) {
        List<View> allchildren = new ArrayList<View>();
        if (view instanceof ViewGroup) {
            ViewGroup vp = (ViewGroup) view;
            for (int i = 0; i < vp.getChildCount(); i++) {
                View viewchild = vp.getChildAt(i);
                viewchild.getClass().getSimpleName();
                allchildren.add(viewchild);
                allchildren.addAll(getAllChildViews(viewchild));
            }
        }
        return allchildren;
    }

    public List<View> getAllChildViewsByName(View view, String name) {
        List<View> allchildren = new ArrayList<View>();
        if (view instanceof ViewGroup) {
            ViewGroup vp = (ViewGroup) view;
            for (int i = 0; i < vp.getChildCount(); i++) {
                View viewchild = vp.getChildAt(i);
                String viewName = viewchild.getClass().getSimpleName();
                checkLayout(viewchild, viewName, allchildren);
            }
        }
        return allchildren;
    }

    public void checkLayout(View viewchild, String viewName,
                            List<View> allchildren) {
        if (viewName.equals("LinearLayout") || viewName.equals("RadioGroup") || viewName.equals("MyRadioGroup")) {
            ViewGroup layoutGroup = (ViewGroup) viewchild;
            for (int j = 0; j < layoutGroup.getChildCount(); j++) {
                View radioGroup = layoutGroup.getChildAt(j);
                String rgName = radioGroup.getClass().getSimpleName();
                if (rgName.equals("RadioButton")) {
                    allchildren.addAll(getAllChildViews(layoutGroup));
                }
                if (rgName.equals("RadioGroup")) {
                    allchildren.addAll(getAllChildViews(radioGroup));
                }
                if (rgName.equals("MyRadioGroup")) {
                    checkLayout(radioGroup, rgName, allchildren);
                }
                if (rgName.equals("LinearLayout")) {
                    checkLayout(radioGroup, rgName, allchildren);
                }
            }
        }
    }

    public void setAllRedioButtonUncheck(Activity activity, int layoutId) {
        List<View> list = new ArrayList<View>();
        list = getAllChildViewsByName(activity.findViewById(layoutId),
                "RadioButton");
        getAllChildViewsByName(activity.findViewById(layoutId), "RadioButton");
        for (int i = 0; i < list.size(); i++) {
            View child = list.get(i);
            String viewName = child.getClass().getSimpleName();
            if (viewName.equals("RadioButton")) {
                RadioButton button = (RadioButton) child;
                button.setChecked(false);
                if (button.getParent().getClass().getSimpleName()
                        .equals(("RadioGroup"))) {
                    RadioGroup radiogroup = (RadioGroup) button.getParent();
                    radiogroup.clearCheck();
                }
            }
        }
    }
}